import {
    toast,
    dateFormat
} from '../../../utils/util'
import {
    queryUserInfo
} from '../../api/insurance'
import dayjs from 'dayjs'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '',
        datec: '',
        id: '',
        userId: '',
        phone: '',
        userName: '',
        comboName: '',
        outDangerDate: '',
        idCardNo: '',
        tbdate: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        const date = dayjs(options.createtime);
        const previousDate = date.add(1, 'day').format('YYYY-MM-DD');
        if (options.name) {
            this.setData({
                id: options.id,
                userId: options.user,
                phone: options.phone,
                userName: options.name,
                comboName: options.comboname,
                tbdate: dayjs(options.createtime).format('YYYY-MM-DD'),
                idCardNo: options.idCardNo,
                datec: previousDate,
                date: dayjs(options.createtime).format('YYYY-MM-DD'),
            })
        }
        // this.setData({
        //     date: time,
        //     // tbdate: time
        // })

    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },
    tbbindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            tbdate: e.detail.value
        })
    },
    // getQueryUserInfo() {
    //     queryUserInfo({
    //         insureDay: this.data.tbdate,
    //         outDangerUserName: '',
    //         insureCompanyType: 'insure_type_rrb'
    //     }).then(res => {
    //         if (res.length === 0) {
    //             this.setData({
    //                 isTips: true
    //             })
    //         } else {
    //             this.setData({
    //                 isTips: false
    //             })
    //         }
    //     })
    // },
    onSearch() {
        if (this.data.date) {
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-search/insurance-search?outDangerDate=' + this.data.tbdate,
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    someEvent: (data) => {
                        console.log(data, 'data')
                        if (data.name) {
                            this.setData({
                                id: data.id,
                                userId: data.user,
                                phone: data.phone,
                                userName: data.name,
                                comboName: data.comboname,
                                tbdate: data.outDangerDate,
                                idCardNo: data.idCardNo
                            })
                        }
                    },
                }
            })
        } else {
            toast('请先选择日期')
        }
    },
    onReportCase() {
        console.log(this.data)
        if (!this.data.userName) {
            toast('请选择出险人')
            return
        }
        if (!this.data.date) {
            toast('请先选择日期')
            return
        }
        // if (this.data.tbdate !== this.data.outDangerDate) {
        //     toast('投保日期必须和出险人日期一样')
        //     return
        // }
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-report-detail/insurance-report-detail',
            success: (res) => {
                res.eventChannel.emit('init', {
                    id: this.data.id,
                    userId: this.data.userId,
                    phone: this.data.phone,
                    userName: this.data.userName,
                    comboName: this.data.comboName,
                    date: this.data.date,
                    idCardNo: this.data.idCardNo
                })
            }
        })

    }
})